'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const f = require('../nodes/Scalar.cjs'),
  w = require('./resolve-end.cjs');
function p(t, a, l) {
  const { offset: e, type: s, source: n, end: i } = t;
  let c, r;
  const d = (b, u, S) => l(e + b, u, S);
  switch (s) {
    case 'scalar':
      (c = f.Scalar.PLAIN), (r = v(n, d));
      break;
    case 'single-quoted-scalar':
      (c = f.Scalar.QUOTE_SINGLE), (r = x(n, d));
      break;
    case 'double-quoted-scalar':
      (c = f.Scalar.QUOTE_DOUBLE), (r = E(n, d));
      break;
    default:
      return (
        l(
          t,
          'UNEXPECTED_TOKEN',
          `Expected a flow scalar value, but found: ${s}`,
        ),
        {
          value: '',
          type: null,
          comment: '',
          range: [e, e + n.length, e + n.length],
        }
      );
  }
  const o = e + n.length,
    h = w.resolveEnd(i, o, a, l);
  return { value: r, type: c, comment: h.comment, range: [e, o, h.offset] };
}
function v(t, a) {
  let l = '';
  switch (t[0]) {
    case '	':
      l = 'a tab character';
      break;
    case ',':
      l = 'flow indicator character ,';
      break;
    case '%':
      l = 'directive indicator character %';
      break;
    case '|':
    case '>': {
      l = `block scalar indicator ${t[0]}`;
      break;
    }
    case '@':
    case '`': {
      l = `reserved character ${t[0]}`;
      break;
    }
  }
  return (
    l && a(0, 'BAD_SCALAR_START', `Plain value cannot start with ${l}`), g(t)
  );
}
function x(t, a) {
  return (
    (t[t.length - 1] !== "'" || t.length === 1) &&
      a(t.length, 'MISSING_CHAR', "Missing closing 'quote"),
    g(t.slice(1, -1)).replace(/''/g, "'")
  );
}
function g(t) {
  let a, l;
  try {
    (a = new RegExp(
      `(.*?)(?<![ 	])[ 	]*\r?
`,
      'sy',
    )),
      (l = new RegExp(
        `[ 	]*(.*?)(?:(?<![ 	])[ 	]*)?\r?
`,
        'sy',
      ));
  } catch {
    (a = /(.*?)[ \t]*\r?\n/sy), (l = /[ \t]*(.*?)[ \t]*\r?\n/sy);
  }
  let e = a.exec(t);
  if (!e) return t;
  let s = e[1],
    n = ' ',
    i = a.lastIndex;
  for (l.lastIndex = i; (e = l.exec(t)); )
    e[1] === ''
      ? n ===
        `
`
        ? (s += n)
        : (n = `
`)
      : ((s += n + e[1]), (n = ' ')),
      (i = l.lastIndex);
  const c = /[ \t]*(.*)/sy;
  return (
    (c.lastIndex = i),
    (e = c.exec(t)),
    s + n + ((e == null ? void 0 : e[1]) ?? '')
  );
}
function E(t, a) {
  let l = '';
  for (let e = 1; e < t.length - 1; ++e) {
    const s = t[e];
    if (
      !(
        s === '\r' &&
        t[e + 1] ===
          `
`
      )
    )
      if (
        s ===
        `
`
      ) {
        const { fold: n, offset: i } = _(t, e);
        (l += n), (e = i);
      } else if (s === '\\') {
        let n = t[++e];
        const i = I[n];
        if (i) l += i;
        else if (
          n ===
          `
`
        )
          for (n = t[e + 1]; n === ' ' || n === '	'; ) n = t[++e + 1];
        else if (
          n === '\r' &&
          t[e + 1] ===
            `
`
        )
          for (n = t[++e + 1]; n === ' ' || n === '	'; ) n = t[++e + 1];
        else if (n === 'x' || n === 'u' || n === 'U') {
          const c = { x: 2, u: 4, U: 8 }[n];
          (l += k(t, e + 1, c, a)), (e += c);
        } else {
          const c = t.substr(e - 1, 2);
          a(e - 1, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${c}`), (l += c);
        }
      } else if (s === ' ' || s === '	') {
        const n = e;
        let i = t[e + 1];
        for (; i === ' ' || i === '	'; ) i = t[++e + 1];
        i !==
          `
` &&
          !(
            i === '\r' &&
            t[e + 2] ===
              `
`
          ) &&
          (l += e > n ? t.slice(n, e + 1) : s);
      } else l += s;
  }
  return (
    (t[t.length - 1] !== '"' || t.length === 1) &&
      a(t.length, 'MISSING_CHAR', 'Missing closing "quote'),
    l
  );
}
function _(t, a) {
  let l = '',
    e = t[a + 1];
  for (
    ;
    (e === ' ' ||
      e === '	' ||
      e ===
        `
` ||
      e === '\r') &&
    !(
      e === '\r' &&
      t[a + 2] !==
        `
`
    );

  )
    e ===
      `
` &&
      (l += `
`),
      (a += 1),
      (e = t[a + 1]);
  return l || (l = ' '), { fold: l, offset: a };
}
const I = {
  '0': '\0',
  'a': '\x07',
  'b': '\b',
  'e': '\x1B',
  'f': '\f',
  'n': `
`,
  'r': '\r',
  't': '	',
  'v': '\v',
  'N': '',
  '_': ' ',
  'L': '\u2028',
  'P': '\u2029',
  ' ': ' ',
  '"': '"',
  '/': '/',
  '\\': '\\',
  '	': '	',
};
function k(t, a, l, e) {
  const s = t.substr(a, l),
    i = s.length === l && /^[0-9a-fA-F]+$/.test(s) ? parseInt(s, 16) : NaN;
  if (isNaN(i)) {
    const c = t.substr(a - 2, l + 2);
    return e(a - 2, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${c}`), c;
  }
  return String.fromCodePoint(i);
}
exports.resolveFlowScalar = p;
